package com.bonc.ioc.service.impl;

import com.bonc.ioc.base.service.BaseService;
import com.bonc.ioc.dao.SysDicInfoMapper;
import com.bonc.ioc.model.DataAssetInfo;
import com.bonc.ioc.model.SysDicInfo;
import com.bonc.ioc.service.SysDicInfoService;
import com.bonc.ioc.vo.DataAssetVo;
import com.bonc.ioc.vo.SysDicInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindException;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @description:
 * @author: wangqinglin
 * @date: 2021/3/18 14:47
 * @change : 2021/3/18 14:47 by q8541q@163.com  for init
 */
@Service
public class SysDicInfoServiceImpl extends BaseService implements SysDicInfoService {

    @Resource
    private SysDicInfoMapper sysDicInfoMapper;

    /**
     * 根据字典类型获取 所有选项
     *
     * @param value
     * @return
     */
    @Override
    public List<SysDicInfoVo> getByType(String value) {
        return sysDicInfoMapper.selectDictionaryListByType(value);
    }

    /**
     * 查询分类字典
     *
     * @param pid 父id
     * @return
     * @throws BindException
     */
    @Override
    public List<SysDicInfo> queryCategory(Integer pid) throws BindException {
        List<SysDicInfo> rows = sysDicInfoMapper.getCategory(pid);
        return rows;
    }

    @Override
    public List<SysDicInfoVo> getDirs() {
        return sysDicInfoMapper.getDirs();
    }

    @Override
    public int save(SysDicInfoVo record) {
        //修改
        if (record.getDicId() != null) {
            fillModifyTimeInfo(record);
            return sysDicInfoMapper.update(record);
        } else {//新增
            if (record.getCreateTime() == null) {
                fillBasicTimeInfo(record);
            }
            fillModifyTimeInfo(record);
            record.setDicType(record.getDicCode());
            String maxNumAndOneCode = sysDicInfoMapper.maxNumAndOneCode(record.getDicParid());
            if (maxNumAndOneCode == null || maxNumAndOneCode == "") {
                record.setDicCode(record.getDicCode() + "001");
            } else {
                record.setDicCode(maxNumAndOneCode);
            }

            Integer maxNumAndOneOrder = sysDicInfoMapper.maxNumAndOneOrder(record.getDicParid());
            if (maxNumAndOneOrder == null) {
                record.setDicOrder(1);
            } else {
                record.setDicOrder(maxNumAndOneOrder);
            }

            return sysDicInfoMapper.save(record);

        }
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        //查询是否有下一级
        int num = sysDicInfoMapper.selectCount(id);
        if (num > 0) {
            return -1;
        }
        return sysDicInfoMapper.deleteByDicId(id);
    }

    private void fillModifyTimeInfo(SysDicInfoVo record) {
        record.setModifyTime(new Date());
    }

    private void fillBasicTimeInfo(SysDicInfoVo record) {
        record.setCreateTime(new Date());
    }

    /**
     * 查询数据来源分类
     *
     * @return
     */
    @Override
    public List<DataAssetInfo> queryDataSource() {
        return sysDicInfoMapper.queryDataSource();
    }
}
